﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using RoboContainer.Infection;

namespace TimeMachine.Engine.Storing.Impl
{
	public interface IMetricValuesStorageDisposition
	{
		string GetMetricsFilename(string metricId);
		IEnumerable<string> GetMetricsIds();
	}

	public class MetricValuesStorageDisposition : IMetricValuesStorageDisposition
	{
		private readonly string basePath;

		[ContainerConstructor]
		public MetricValuesStorageDisposition(IProjectSettings projectSettings)
			:this(projectSettings.GetDataDirPath())
		{
		}

		public MetricValuesStorageDisposition(string dataDirPath)
		{
			basePath = dataDirPath;
			Directory.CreateDirectory(basePath);
		}

		public string GetMetricsFilename(string metricId)
		{
			return Path.Combine(basePath, metricId + ".metrics.xml");
		}

		public IEnumerable<string> GetMetricsIds()
		{
			return 
				Directory.GetFiles(basePath, "*.metrics.xml")
				.Select(filepath => Path.GetFileName(filepath))
				.Select(filename => filename.Substring(0, filename.Length - ".metrics.xml".Length));
		}
	}
}